package com.neuedu.web.service;

import com.neuedu.web.common.PageInfo;
import com.neuedu.web.dao.MenuDao;
import com.neuedu.web.po.MenuPO;
import com.neuedu.web.po.MenuPO;
import com.neuedu.web.po.UmsUser;
import com.neuedu.web.utils.BeanConvertUtils;
import com.neuedu.web.utils.ParamUtils;
import com.neuedu.web.vo.DeptVO;
import com.neuedu.web.vo.MenuVO;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 金山
 * 项目：javaweb
 * site: https://blog.fulfill.com.cn
 * 描述  Dept 业务工呢
 * @data 2024年11月21日14:23:14
 */
public class MenuService {

    private MenuDao menuDao = new MenuDao();


    public List list(HttpServletRequest request, HttpServletResponse response) throws IOException, NoSuchFieldException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {


        String sql = " select * from ums_menu";
        List<MenuPO> list = menuDao.selectAll(sql);

        return BeanConvertUtils.po2Vo(list, MenuPO.class);

    }


    public List listByParentId(HttpServletRequest request, HttpServletResponse response) throws IOException, NoSuchFieldException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {

        String paretnId = request.getParameter("parentId");
        Integer numParentId   = 0;
        if(paretnId != null){
            numParentId = Integer.parseInt(paretnId);
        }
        String sql = " select * from ums_menu where parent_id = ? ";
        List<MenuPO> list = menuDao.selectAll(sql,numParentId);

        return BeanConvertUtils.po2Vo(list, MenuPO.class);

    }


    public PageInfo<MenuVO> page(HttpServletRequest request, HttpServletResponse response) throws NoSuchFieldException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {


        String sql = " select * from ums_menu";


        PageInfo pageInfo = ParamUtils.getPageInfo(request);
        List<MenuPO> dataList = menuDao.selectPage(pageInfo.getCurrent(), pageInfo.getSize(), sql);
        int total = menuDao.total(sql);

        pageInfo.setTotal(total);

        // 数据
        pageInfo.setData(BeanConvertUtils.po2Vo(dataList, MenuVO.class));

        return pageInfo;

    }

    /**
     * 保存
     * @param request
     * @param response
     * @return
     */
    public boolean save(HttpServletRequest request, HttpServletResponse response) {
        MenuPO data = ParamUtils.getParam(MenuPO.class, request);
        if(data.getMenuId() != null){
            return  menuDao.update(data) >0;

        }else{
            return  menuDao.insert(data) >0;

        }
    }

    public boolean delete(HttpServletRequest request, HttpServletResponse response) {
        String id = request.getParameter("id");

        return menuDao.delete(Long.parseLong(id)) >0;
    }


    public MenuVO getById(HttpServletRequest request, HttpServletResponse response) throws NoSuchFieldException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
        String id = request.getParameter("id");

        return BeanConvertUtils.conver(MenuVO.class,menuDao.selectById(Long.parseLong(id))) ;
    }

    public List<MenuVO> getMenuTree(HttpServletRequest request, HttpServletResponse response) throws NoSuchFieldException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
            /**
             *
             {

             }
             */

            List resultMenuList = new ArrayList();




        String sql = " select * from ums_menu";
        List<MenuPO> list = menuDao.selectAll(sql);

        for (MenuPO menuPO : list) {
            int parentId = menuPO.getParentId().intValue();
            if(parentId == 0){
                //系统管理

                getChildren(list,menuPO);

                resultMenuList.add(menuPO);
            }
        }

        return resultMenuList;

    }



    public List<MenuVO> getMenuTreeByUserId(HttpServletRequest request, HttpServletResponse response) throws NoSuchFieldException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {


        HttpSession session = request.getSession();
        UmsUser loginUser = (UmsUser) session.getAttribute("user");


        List resultMenuList = new ArrayList();

        StringBuffer sql = new StringBuffer();
        sql.append("  SELECT                                      ");
        sql.append("     DISTINCT m.*                 ");
        sql.append("  FROM                ");
        sql.append("    ums_menu m      ");
        sql.append("    LEFT JOIN role_menu rm      ");
        sql.append("      ON m.`menu_id` = rm.menu_id   ");
        sql.append("            ");
        sql.append("     LEFT JOIN user_role ur ON  rm.role_id = ur.role_id  ");
        sql.append("    WHERE  ur.userid = ?   ");

        //根据用户身份查询所有的菜单项目
        List<MenuPO> list = menuDao.selectAll(sql.toString() ,loginUser.getUserid());

        for (MenuPO menuPO : list) {
            int parentId = menuPO.getParentId().intValue();
            if(parentId == 0){
                //系统管理

                getChildren(list,menuPO);

                resultMenuList.add(menuPO);
            }
        }

        return resultMenuList;

    }





    private void getChildren(List<MenuPO> list, MenuPO parent) {
        for (MenuPO po : list) {
            //po  用户管理  parent ID 1
            //parent 系统管理  1
            if(po.getParentId().equals(parent.getMenuId())){
                parent.getChildren().add(po);
            }
        }
    }
}
